<?php

Yii::import('common.models._base.BaseShoppingCartByUser');

class ShoppingCartByUser extends BaseShoppingCartByUser {

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }
    
    
      public static function label($n = 1) {
        return Yii::t('app', 'Shopping Cart By User|Shopping Cart By Users', $n);
    }
  public function attributeLabels() {
        return array(
            'id' => Yii::t('app', 'ID'),
            'user_id' => Yii::t('app', 'Username'),            
            'class_id' => Yii::t('app', 'Class'),
            'course_id' => Yii::t('app', 'Course'),
            'batch_id' => Yii::t('app', 'Batch'),
            'qty' => Yii::t('app', 'Qty'),
            'batchtime' => Yii::t('app', 'Batchtime'),
            'batchdate' => Yii::t('app', 'Batchdate'),
            'total_amt' => Yii::t('app', 'Total Amt'),
            'discount_txt' => Yii::t('app', 'Discount Txt'),
            'gift_to_user_email' => Yii::t('app', 'Gift To User Email'),
        );
    }
public function relations() {
        return array(
           'user' => array(self::BELONGS_TO, 'Users', 'user_id'),
           
           
        );
    }
    public static function saveCartbyUser($userid) {
        $checkdata = ShoppingCartByUser::model()->findAllbyAttributes(array('user_id' => $userid));
        if ($checkdata) {
            ShoppingCartByUser::model()->deleteAll('user_id=:user_id', array(':user_id' => $userid));
        }
        $AllDataCart = Yii::app()->shoppingCart->getPositions();
        if ($AllDataCart <> "") {
            foreach ($AllDataCart as $AllDataCart) {
                $discount_price = ClassBatch::getBatchPriceBySeatsInt($AllDataCart->id, $AllDataCart->quantity, 'checkout');
                $modelShoppingCartByUser = new ShoppingCartByUser;
                $modelShoppingCartByUser->setAttribute('user_id', $userid);
                $modelShoppingCartByUser->setAttribute('class_id', $AllDataCart->classes_id);
                if (isset($AllDataCart->courses_id))
                    $modelShoppingCartByUser->setAttribute('course_id', $AllDataCart->courses_id);
                $modelShoppingCartByUser->setAttribute('batch_id', $AllDataCart->id);
                $modelShoppingCartByUser->setAttribute('qty', $AllDataCart->quantity);
                $modelShoppingCartByUser->setAttribute('batchtime', $AllDataCart->batchtiming);
                $modelShoppingCartByUser->setAttribute('batchdate', $AllDataCart->startdate);
                $modelShoppingCartByUser->setAttribute('total_amt', $AllDataCart->batchprice);
                $modelShoppingCartByUser->setAttribute('gift_to_user_email', $AllDataCart->giftuseremail);
                $modelShoppingCartByUser->setAttribute('discount_txt', $discount_price);
                $modelShoppingCartByUser->save(false);
            }
        }
    }

    public static function EBS_secure_hash($subtotal) {
        $secretKey = Message::SECRETKEY;
        $account_id = Message::ACCOUNT_ID;
        $amount = $subtotal;
        $refrence_no = Message::REFRENCE_NO;
        $return_url = Message::RETURN_URL;
        $mode = Message::MODE;
        $string = "$secretKey|$account_id|$amount|$refrence_no|$return_url|$mode";
        return $secure_hash = md5($string);
    }

    public static function putShoppingCart() {
        $checkdata = ShoppingCartByUser::model()->findAllbyAttributes(array('user_id' => Yii::app()->user->id));
        if ($checkdata) {
            foreach ($checkdata as $checkdata) {
                if ($checkdata->course_id > 0) {
                    $ClassBatch = CourseBatche::model()->findByPk($checkdata->batch_id);
                } else {
                    $ClassBatch = ClassBatch::model()->findByPk($checkdata->batch_id);
                }
                Yii::app()->shoppingCart->put($ClassBatch, $checkdata->qty, $checkdata->batchdate, $checkdata->batchtime, $checkdata->total_amt,$checkdata->gift_to_user_email);
            }
        }
    }

}